//
//  SellRecodeTableViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 16/11/14.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "SellRecodeTableViewController.h"
#import "SellRecodeTableViewCell.h"
#import "SellRecodeModel.h"
#import "SellRecodeDetailViewController.h"
#import "SellRecordSelectedView.h"
@interface SellRecodeTableViewController ()<UITableViewDelegate,UITableViewDataSource,didClickSelectedSellRecodeButtonDelegate>

{
    MBProgressHUD *progressHUD;
    NSString *_orderType;
     NSInteger _indexPage;
}

@property (nonatomic,strong)NSMutableArray *sellRecodeArray;

@property (nonatomic,strong)UITableView *SellTableView;

@property (nonatomic,strong)SellRecordSelectedView *selectedView;

@end

@implementation SellRecodeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"销售订单";
    self.view.backgroundColor = BACKGROUND_COLOR;
//    self.tableView.backgroundColor = BACKGROUND_COLOR;
//    self.tableView.tableFooterView = [[UIView alloc] init];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSelectedView];
    
    [self addSellTableView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    _indexPage = 1;
    
    self.SellTableView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreSellRecode)];
}

- (void)viewWillAppear:(BOOL)animated {
 
    [super viewWillAppear:animated];
    [self loadSellRecodeData];
    [self showHudInView:self.view hint:@"正在加载中..."];
}

- (void)addSelectedView {
    SellRecordSelectedView * SelectedView = [[SellRecordSelectedView alloc] initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT * 0.07)];
    SelectedView.delegate = self;
    self.selectedView = SelectedView;
    [self.view addSubview:SelectedView];
}

- (void)addSellTableView {
    CGFloat y = CGRectGetMaxY(self.selectedView.frame);
        NSLog(@"%f",y);
    self.SellTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, WIDTH, HEIGHT - y - 20 - HEIGHT * 0.07)style:UITableViewStylePlain];
    self.SellTableView.delegate = self;
    self.SellTableView.dataSource = self;
    self.SellTableView.backgroundColor = BACKGROUND_COLOR;
    self.SellTableView.tableFooterView = [[UIView alloc] init];
    self.SellTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.SellTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.SellTableView];

}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"%s",__func__);
    [progressHUD hide:YES];
    if (progressHUD.removeFromSuperViewOnHide) {
        [progressHUD removeFromSuperview];
    }
    [self.sellRecodeArray removeAllObjects];
    [self.SellTableView reloadData];
    [self hideHud];

}

- (void)didClickSelectedSellRecodeButton:(UIButton *)button {
    NSLog(@"%ld",button.tag);
    
    // 1已发货  2未发货
    if (button.tag == 1000) {
        _orderType = @"1";
    } else {
        _orderType = @"0";
    }
    
    _indexPage = 1;
    [self.SellTableView.mj_footer resetNoMoreData];
    [self showHudInView:self.view hint:@"正在加载中..."];
    [self.sellRecodeArray removeAllObjects];
    [self.SellTableView reloadData];
    [self loadSellRecodeData];
}

- (void)loadMoreSellRecode {
    _indexPage++;
    [self loadSellRecodeData];
}

- (void)loadSellRecodeData {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSDictionary *params = @{
                                 @"username":USERNAME,
                                 @"page":[NSString stringWithFormat:@"%ld",_indexPage]
                                 };
        NSLog(@"%@",params);
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/user/myother" completeBlock:^(id object) {
            NSLog(@"%@",object);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self hideHud];
            });
            if (JudgeBackDataCode) {
                
                NSArray *array = [object objectForKey:@"data"];
                
                for (NSDictionary *dict in array) {
                    SellRecodeModel *cooperate = [[SellRecodeModel alloc] initWithDictionary:dict error:nil];
                    
                    if (_orderType.length) {
                        
                        if ([cooperate.kuaidi isEqualToString:_orderType]) {
                            [self.sellRecodeArray addObject:cooperate];
                        }
                        
                    } else {
                        
                        if (cooperate) {
                            [self.sellRecodeArray addObject:cooperate];
                        }
                    }
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.SellTableView reloadData];
                    [self.SellTableView.mj_footer endRefreshing];
                });
                
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSDictionary *msg = [object objectForKey:@"message"];
                    MBProgressHUD *hud = [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@",msg[@"view"]] toView:self.view dimBackground:NO AndCompleteBlock:nil];
                    [self.view addSubview:hud];
                    [self.SellTableView.mj_footer endRefreshingWithNoMoreData];
                });
            }
            
        }];
    });
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sellRecodeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    SellRecodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[SellRecodeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    SellRecodeModel *model = self.sellRecodeArray[indexPath.row];
    cell.recodeModel = model;
    
    cell.pushButton.tag = indexPath.row;
    [cell.pushButton addTarget:self action:@selector(didClickSellRecordPushButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}

- (void)didClickSellRecordPushButton:(UIButton *)button {
    NSLog(@"%ld",button.tag);
    SellRecodeModel *model = self.sellRecodeArray[button.tag];
    
    NSDictionary *params = @{
                             @"dxh":model.dxh,
                             @"username":USERNAME
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/user/orderinfo" completeBlock:^(id object) {
       
        if (JudgeLogInData) {
            SellRecodeDetailViewController *detail = [[SellRecodeDetailViewController alloc] init];
            detail.orderNumber = model.dxh;
            [self.navigationController pushForm:self.navigationController pushToAndHidden:detail];
        } else {
            NSDictionary *msg = [object objectForKey:@"message"];
            MBProgressHUD *hud = [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@",msg[@"view"]] toView:self.view dimBackground:NO AndCompleteBlock:nil];
            [self.view addSubview:hud];
        }
        
    }];
    
    
}


- (NSMutableArray *)sellRecodeArray {
    if (!_sellRecodeArray) {
        _sellRecodeArray = [NSMutableArray array];
    }
    return _sellRecodeArray;
}

@end
